//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "REZERV.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IB_Services"
#pragma link "pFIBErrorHandler"
#pragma resource "*.dfm"
TfrREZERV *frREZERV;
//---------------------------------------------------------------------------
__fastcall TfrREZERV::TfrREZERV(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::ErrorHandler1FIBErrorEvent(TObject *Sender,
      EFIBError *ErrorValue, TKindIBError KindIBError, bool &DoRaise)
{
Application->MessageBox(("Error by batabase backup\nIBErrorCode: " +
  IntToStr(ErrorValue->IBErrorCode) + '\n' +
  "IBMessage: " + ErrorValue->IBMessage).c_str(),
      "Error", MB_OK | MB_ICONSTOP);
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::BExitClick(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::BBackupClick(TObject *Sender)
{
TCursor OldCursor = Screen->Cursor;
Screen->Cursor = crHourGlass;
BackupService1->DatabaseName = TDatabase->Text;
if (Trim(CBServerName->Text) != "")
{ BackupService1->ServerName = CBServerName->Text;
  switch (CBProtocol->ItemIndex)
  { case 0: BackupService1->Protocol = Local;
    case 1: BackupService1->Protocol = NamedPipe;
    case 2: BackupService1->Protocol = SPX;
    case 3: BackupService1->Protocol = TCP;
  }
}
else
{ BackupService1->ServerName = "";
  BackupService1->Protocol = Local;
}
BackupService1->Params->Clear();
BackupService1->Params->Add("user_name=" + TUserName->Text);
BackupService1->Params->Add("password=" + TPassword->Text);
BackupService1->BackupFile->Clear();
for (int i = 0; i < ListBox1->Items->Count; i++)
  BackupService1->BackupFile->Add(ListBox1->Items->Strings[i]);
Memo1->Clear();
try
{ BackupService1->Active = true;
  BackupService1->ServiceStart();
}
catch (...)
{ BackupService1->Active = false;
  Screen->Cursor = OldCursor;
  return;
}
BExit->Enabled = false;
BBackup->Enabled = false;
Memo1->Lines->Add(
  "==================== Backup begins ====================");
while (!BackupService1->Eof)
  Memo1->Lines->Add(BackupService1->GetNextLine());
BackupService1->Active = false;
BExit->Enabled = true;
BBackup->Enabled = true;
Memo1->Lines->Add(
  "=================== Backup ends =======================");
Screen->Cursor = OldCursor;
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::BOpenDatabaseClick(TObject *Sender)
{
if (!OpenDialogDatabase->Execute()) return;
TDatabase->Text = OpenDialogDatabase->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::BOpenBackupClick(TObject *Sender)
{
if (!OpenDialogBackup->Execute()) return;
TBackup->Text = OpenDialogBackup->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::TDatabaseChange(TObject *Sender)
{
BBackup->Enabled = (Trim(TDatabase->Text) != "") &&
  (Trim(TUserName->Text) != "") && (Trim(TPassword->Text) != "") &&
  (ListBox1->Items->Count > 0);
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::CBServerChange(TObject *Sender)
{
if (CBServer->ItemIndex == 0)   // Local
{ LServerName->Visible = false;
  CBServerName->Visible = false;
  CBProtocol->Visible = false;
  LProtocol->Visible = false;
}
else                           // Remote
{ LServerName->Visible = true;
  CBServerName->Visible = true;
  CBProtocol->Visible = true;
  LProtocol->Visible = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::BAddBackupClick(TObject *Sender)
{
ListBox1->Items->Add(TBackup->Text + "=" + TLength2->Text);
ListBox1->ItemIndex = ListBox1->Items->Count - 1;
TDatabaseChange(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::TBackupChange(TObject *Sender)
{
BAddBackup->Enabled = Trim(TBackup->Text) != "";
}
//---------------------------------------------------------------------------
void __fastcall TfrREZERV::BDeleteBackupClick(TObject *Sender)
{
int i = ListBox1->ItemIndex;
if (i != -1)
  ListBox1->Items->Delete(i);
ListBox1->ItemIndex = i;
if ((ListBox1->ItemIndex > ListBox1->Items->Count - 1) ||
    (ListBox1->ItemIndex < 0))
  ListBox1->ItemIndex = ListBox1->Items->Count - 1;
}
//---------------------------------------------------------------------------
